{layout name="../../admin/view/layout/main" /}
{:breadcrumb(['UI 示例:example/layout/index', '上传插件:example/upload/index', 'FormData上传'])}
<div class="row">

    <div class="col-lg-12">
      <div class="card">
        <header class="card-header">
          <div class="card-title">H5图片上传</div>
        </header>
        <div class="card-body">
        
        <p>这里采用FormData实现上传效果，示例为单张图片上传。</p>
        <form action="#!" method="post" name="edit-form" class="edit-form">
          <div class="mb-3 file-group">
            <label for="web_site_logo" class="form-label">LOGO图片</label>
            <div class="input-group">
              <input type="text" class="form-control file-value" name="web_site_logo" value="" placeholder="LOGO图片地址" />
              <input type="file" accaccept=".png,.jpg,.jpeg,.bmp,.gif" class="d-none" />
              <div class="input-group-append">
                <button class="btn btn-default file-browser" type="button">上传图片</button>
              </div>
            </div>
          </div>
        </form>
        
        </div>
      </div>
  </div>

</div>

<script type="text/javascript">
    $(document).ready(function() {
        $(document).on('click', '.file-browser', function() {
            var $browser = $(this);
            var file = $browser.closest('.file-group').find('[type="file"]');
            file.on( 'click', function(e) {
                e.stopPropagation();
            });
            file.trigger('click');
        });
        
        $(document).on('change', '.file-group [type="file"]', function() {
            var $this    = $(this);
            var $input   = $(this)[0];
            var $len     = $input.files.length;
            var formFile = new FormData();
            
            if ($len == 0) {
                return false;
            } else {
                var fileAccaccept = $this.attr('accaccept');
                var fileType      = $input.files[0].type;
                var type          = (fileType.substr(fileType.lastIndexOf("/") + 1)).toLowerCase();
                
                if (!type || fileAccaccept.indexOf(type) == -1) {
                    showNotify('您上传图片的类型不符合(.jpg|.jpeg|.gif|.png|.bmp)', 'danger', 1000);
                    return false;
                }
                formFile.append("file", $input.files[0]);
            }
            
            var data = formFile;
            var l = $('body').lyearloading({
                opacity: 0.2,
                spinnerSize: 'nm'
            });
            
            $.ajax({
                url: "{:url('example/upload/upload_data')}",
                data: data,
                type: "POST",
                dataType: "json",
                //上传文件无需缓存
                cache: false,
                //用于对data参数进行序列化处理 这里必须false
                 processData: false,
                //必须
                contentType: false, 
                success: function (res) {
                    l.destroy();
                    if (res.status === 1) {
                        $this.closest('.file-group').find('.file-value').val(res.url);
                    } else {
                        showNotify(res.info, 'danger', 3000);
                    }
                },
            });
        });
    });
    </script>